import java.util.Arrays;

class list {

    static int DEFUALT = 5;
    static int count = 0;//什么都没有储存，哨兵
    static int ptr_ed;//末尾位置
    static list[] arr = new list[DEFUALT];
    private int next = -1;
    private int val = 0;

    static {
        arr[0] = new list();
        count += 1;
        ptr_ed = 0;
    }

    private void isFull() {
        if (count == DEFUALT) {
            list[] tmp = Arrays.copyOf(arr, DEFUALT * 2);
            arr = tmp;
            DEFUALT *= 2;
        }
    }

    public void addVal(int val) {
        isFull();//判满

        list tmp = new list();
        arr[count] = tmp;
        arr[count].val = val;
        arr[ptr_ed].next = count;//沿着数组储存
        ptr_ed = count;//ptr 为下一个数组的位置
        count++;
    }

    public void addVal(int val, int index) {
        isFull();//判满

        list tmp = new list();
        arr[count] = tmp;//实际上也是沿着数组插入
        arr[count].val = val;
        //更改指向
        int index_tmp = arr[index].next;
        arr[index].next = count;//index -> now
        arr[count].next = index_tmp;//now-> index.next
        count++;//ok
    }

    public void setVal(int val, int index) {
        arr[index].val = val;
    }
    public int find(int target) {
        int i = 0;
        i = arr[i].next;//将哨兵为过滤
        for (int j = 0; j < count; j++) {
            if (arr[i].val == target) {
                return i;
            }
            i = arr[i].next;//下一节点
        }
        return -1;
    }

    public void pop() {//尾删
        int ed = 0;
        int i = 0;
        i = arr[i].next;
        for (int j = 0; j < count - 1; j++) {//找到尾
            ed = i;//ed.next 为尾巴
            i = arr[i].next;
            if (i == -1) {
                break;
            }
        }
        ptr_ed = ed;//末尾
        arr[ed].next = -1;
        count--;
        if (count == 1) {//没有元素了，重新开始
            ptr_ed = 0;//末尾
            arr[ed].next = -1;
        }
    }

    public void pop(int index) {//删除指定下标
        //找到前一个的下标
        int target = 0;
        int i = 0;
        i = arr[i].next;//移除哨兵
        //隔离了index
        if (index != 1) {
            for (int j = 0; j < count - 1; j++) {//找到尾
                if (arr[i].next == index) {//target的上一个节点
                    target = i;
                    break;
                }
                i = arr[i].next;
            }
        }
        arr[target].next = arr[index].next;//隔离了index
        count--;//ok
    }
    public int getVal(int index) {
        return arr[index].val;
    }

    public int getCount() {
        return count - 1;//有效数据
    }

    public void show() {
        int i = 0;
        i = arr[i].next;//
        for (int j = 0; j < count - 1; j++) {//找到尾
            System.out.print(arr[i].val+" ");
            i = arr[i].next;
            if (i == -1) {
                break;
            }
        }
        System.out.print("\n count = "+ (count-1)+"\n");
    }
}

public class one {

    public static void main(String[] args) {
        list t = new list();
        for (int i = 0; i < 6; i++) {
            t.addVal(i);
        }
        t.setVal(999, 1);
        t.show();
    }
    public static void main3(String[] args) {
        list t = new list();
        for (int i = 0; i < 10; i++) {
            t.addVal(i);
        }
        t.show();
        for (int i = 0; i < 14; i++) {
            t.addVal(i, i);
        }
        t.show();
        for (int i = 0; i < 24; i++) {
            t.pop();
        }
        System.out.println(t.getCount());
        t.show();
        for (int i = 0; i < 10; i++) {
            t.addVal(i);
        }
        t.show();
        for (int i = 0; i < 10; i++) {
            t.pop();
        }
        t.show();
        for (int i = 0; i < 10; i++) {
            t.addVal(i);
        }
        t.show();
    }

    public static void main2(String[] args) {
        list t = new list();
        for (int i = 0; i < 10; i++) {
            t.addVal(i);
        }
        t.show();
        System.out.println("尾删");
        t.pop();
        t.show();
        //  t.addVal(111);
        // t.show();
        System.out.println("头删");
        t.pop(t.find(1));
        t.show();
        t.pop(t.find(8));
        t.show();
        System.out.println(t.getCount());
        for (int i = 0; i < 7; i++) {
            t.pop();
        }
        t.show();
    }

    public static void main1(String[] args) {
//        list[] wo = new list[2];
//        System.out.println(Arrays.toString(wo));
        list t = new list();
        System.out.println("count = " + t.getCount());
        t.addVal(66);
        System.out.println("count = " + t.getCount());
        t.addVal(55);
        System.out.println("count = " + t.getCount());
        t.addVal(66);
        t.addVal(66);
        System.out.println("count = " + t.getCount());
        t.show();
        t.addVal(555, 0);
        t.show();
        System.out.println(t.find(66));
        System.out.println(t.getVal(t.find(66)));
        System.out.println(t.getVal(t.find(555)));
        t.addVal(1234, t.find(555));
        t.show();

    }
}
